package com.yxkj.railservicer.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yxkj.railservicer.R;
import com.yxkj.railservicer.bean.QuHuoBean;
import com.yxkj.railservicer.bean.SubOrderDetailBean;
import com.yxkj.railservicer.pager.QuHuo;
import com.yxkj.railservicer.util.AppUtils;
import com.yxkj.railservicer.util.ContentUtil;
import com.yxkj.railservicer.view.MyGridView;

import org.w3c.dom.Text;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by sunyu on 2017/2/28.
 */

public class QuHuoAdapter extends RecyclerView.Adapter<QuHuoAdapter.MyViewHolder> {
    private Context context;
    private List<QuHuoBean> quHuoBeanList;
    private QuHuo quHuo;

    private boolean isRefused = false;

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_quhuo_firstTime;
        TextView tv_quhuo_time;
        TextView tv_quhuo_lasttime;
        TextView tv_quhuo_order;
        ImageView iv_quhuo_icon;
        TextView tv_quhuo_name;
        TextView tv_quhuo_address_get;
        TextView tv_quhuo_address_post;
        Button btn_quhuo_tel;
        Button btn_quhuo;

        MyGridView gv_goods;
        TextView tv_total;

        ImageView iv_status;
        ImageView iv_cancel;
        TextView tv_status;
        TextView tv_cancel;
        TextView tv_quhuo_status;
        TextView tv_quhuo_cancel;
        Button btn_confirm;
        Button btn_cancel;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_quhuo_firstTime = (TextView) itemView.findViewById(R.id.tv_quhuo_firstTime);
            tv_quhuo_time = (TextView) itemView.findViewById(R.id.tv_quhuo_time);
            tv_quhuo_lasttime = (TextView) itemView.findViewById(R.id.tv_quhuo_lasttime);
            iv_quhuo_icon = (ImageView) itemView.findViewById(R.id.iv_quhuo_icon);
            tv_quhuo_name = (TextView) itemView.findViewById(R.id.tv_quhuo_name);
            tv_quhuo_address_get = (TextView) itemView.findViewById(R.id.tv_quhuo_address_get);
            tv_quhuo_address_post = (TextView) itemView.findViewById(R.id.tv_quhuo_address_post);
            tv_quhuo_order = (TextView) itemView.findViewById(R.id.tv_quhuo_order);

            btn_quhuo_tel = (Button) itemView.findViewById(R.id.btn_quhuo_tel);
            btn_quhuo = (Button) itemView.findViewById(R.id.btn_quhuo);

            gv_goods = (MyGridView) itemView.findViewById(R.id.gv_goods);
            tv_total = (TextView) itemView.findViewById(R.id.tv_total);

            iv_status = (ImageView) itemView.findViewById(R.id.iv_status);
            iv_cancel = (ImageView) itemView.findViewById(R.id.iv_cancel);
            tv_status = (TextView) itemView.findViewById(R.id.tv_status);
            tv_cancel = (TextView) itemView.findViewById(R.id.tv_cancel);
            tv_quhuo_status = (TextView) itemView.findViewById(R.id.tv_quhuo_status);
            tv_quhuo_cancel = (TextView) itemView.findViewById(R.id.tv_quhuo_cancel);
            btn_confirm = (Button) itemView.findViewById(R.id.btn_confirm);
            btn_cancel = (Button) itemView.findViewById(R.id.btn_cancel);

        }
    }

    public QuHuoAdapter(Context context, List<QuHuoBean> quHuoBeanList, QuHuo quHuo) {
        this.context = context;
        this.quHuoBeanList = quHuoBeanList;
        this.quHuo = quHuo;
    }

    @Override
    public QuHuoAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_quhuo, parent, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final QuHuoAdapter.MyViewHolder holder, final int position) {
        QuHuoBean quHuoBean = quHuoBeanList.get(position);


        holder.tv_quhuo_firstTime.setText(quHuoBean.getFirst_time());
        holder.tv_quhuo_time.setText(quHuoBean.getTime());
        holder.tv_quhuo_lasttime.setText(quHuoBean.getLast_time());
        holder.tv_quhuo_order.setText(quHuoBean.getOrder());
        Picasso.with(context).load(quHuoBean.getIcon()).into(holder.iv_quhuo_icon);
        holder.tv_quhuo_name.setText(quHuoBean.getName());
        holder.tv_quhuo_address_get.setText(quHuoBean.getAddressGet());
        holder.tv_quhuo_address_post.setText(quHuoBean.getAddressPost());

        List<SubOrderDetailBean> foodList = quHuoBean.getFoodList();
//        String distributionCosts = quHuoBean.getDistributionCosts();
//        if(distributionCosts != null){
//            BigDecimal price = new BigDecimal(distributionCosts);
//            foodList.add(new SubOrderDetailBean("配送费",1,price));
//        }
        GridGoodsAdapter goodsAdapter = new GridGoodsAdapter(context, foodList);
        holder.gv_goods.setAdapter(goodsAdapter);

        holder.tv_total.setText("￥" + quHuoBean.getTotal());

        holder.tv_quhuo_cancel.setText(quHuoBean.getCancelReason());

        holder.btn_quhuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(context)
                        .setTitle("提示")
                        .setMessage("确定已取货？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                quHuo.getOrderGoods(position);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();

            }
        });

        holder.btn_quhuo_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quHuo.phone(position);
            }
        });

        if ("9010".equals(quHuoBean.getOrderCancelStatus())) {
            holder.iv_status.setVisibility(View.VISIBLE);
            holder.iv_cancel.setVisibility(View.VISIBLE);
            holder.tv_status.setVisibility(View.VISIBLE);
            holder.tv_cancel.setVisibility(View.VISIBLE);
            holder.tv_quhuo_status.setVisibility(View.VISIBLE);
            holder.tv_quhuo_cancel.setVisibility(View.VISIBLE);
            holder.btn_confirm.setVisibility(View.VISIBLE);
            holder.btn_cancel.setVisibility(View.VISIBLE);

            holder.tv_quhuo_status.setText("用户申请取消订单");

            holder.btn_quhuo.setVisibility(View.GONE);
        } else {
            holder.iv_status.setVisibility(View.GONE);
            holder.iv_cancel.setVisibility(View.GONE);
            holder.tv_status.setVisibility(View.GONE);
            holder.tv_cancel.setVisibility(View.GONE);
            holder.tv_quhuo_status.setVisibility(View.GONE);
            holder.tv_quhuo_cancel.setVisibility(View.GONE);
            holder.btn_confirm.setVisibility(View.GONE);
            holder.btn_cancel.setVisibility(View.GONE);

            holder.btn_quhuo.setVisibility(View.VISIBLE);

        }

        holder.btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quHuo.confirm(position);
                notifyDataSetChanged();
            }
        });

        holder.btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(context, "拒绝", Toast.LENGTH_SHORT).show();
                AppUtils.showMsgDialog(context, new AppUtils.MsgCallBackData() {
                    @Override
                    public void clickMsg(Dialog d, String data) {
                        quHuo.cancel(position, data);
                        d.dismiss();
                    }
                });


            }
        });

    }

    @Override
    public int getItemCount() {
        return quHuoBeanList.size();
    }

}
